home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C & C++ Multimedia Cyber Classroom
/
C and C++ Multimedia Cyber Classroom (Prentice Hall) (1998).iso
/
cpphtp2
/
code.jar
/
code
/
ch07
/
fig07_04.txt
< prev
next >
Wrap
Text File
|
1998-02-27
|
5KB
|
162 lines
1 // Fig. 7.4: date1.h
2 // Declaration of the Date class.
3 // Member functions defined in date1.cpp
4 #ifndef DATE1_H
5 #define DATE1_H
6
7 class Date {
8 public:
9 Date( int = 1, int = 1, int = 1900 ); // default constructor
10 void print() const; // print date in month/day/year format
11 ~Date(); // provided to confirm destruction order
12 private:
13 int month; // 1-12
14 int day; // 1-31 based on month
15 int year; // any year
16
17 // utility function to test proper day for month and year
18 int checkDay( int );
19 };
20
21 #endif
22 // Fig. 7.4: date.cpp
23 // Member function definitions for Date class.
24 #include <iostream.h>
25 #include "date1.h"
26
27 // Constructor: Confirm proper value for month;
28 // call utility function checkDay to confirm proper
29 // value for day.
30 Date::Date( int mn, int dy, int yr )
31 {
32 if ( mn > 0 && mn <= 12 ) // validate the month
33 month = mn;
34 else {
35 month = 1;
36 cout << "Month " << mn << " invalid. Set to month 1.\n";
37 }
38
39 year = yr; // should validate yr
40 day = checkDay( dy ); // validate the day
41
42 cout << "Date object constructor for date ";
43 print(); // interesting: a print with no arguments
44 cout << endl;
45 }
46
47 // Print Date object in form month/day/year
48 void Date::print() const
49 { cout << month << '/' << day << '/' << year; }
50
51 // Destructor: provided to confirm destruction order
52 Date::~Date()
53 {
54 cout << "Date object destructor for date ";
55 print();
56 cout << endl;
57 }
58
59 // Utility function to confirm proper day value
60 // based on month and year.
61 // Is the year 2000 a leap year?
62 int Date::checkDay( int testDay )
63 {
64 static const int daysPerMonth[ 13 ] =
65 {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
66
67 if ( testDay > 0 && testDay <= daysPerMonth[ month ] )
68 return testDay;
69
70 if ( month == 2 && // February: Check for leap year
71 testDay == 29 &&
72 ( year % 400 == 0 || // year 2000?
73 ( year % 4 == 0 && year % 100 != 0 ) ) ) // year 2000?
74 return testDay;
75
76 cout << "Day " << testDay << " invalid. Set to day 1.\n";
77
78 return 1; // leave object in consistent state if bad value
79 }
80 // Fig. 7.4: emply1.h
81 // Declaration of the Employee class.
82 // Member functions defined in emply1.cpp
83 #ifndef EMPLY1_H
84 #define EMPLY1_H
85
86 #include "date1.h"
87
88 class Employee {
89 public:
90 Employee( char *, char *, int, int, int, int, int, int );
91 void print() const;
92 ~Employee(); // provided to confirm destruction order
93 private:
94 char firstName[ 25 ];
95 char lastName[ 25 ];
96 const Date birthDate;
97 const Date hireDate;
98 };
99
100 #endif
101 // Fig. 7.4: emply1.cpp
102 // Member function definitions for Employee class.
103 #include <iostream.h>
104 #include <string.h>
105 #include "emply1.h"
106 #include "date1.h"
107
108 Employee::Employee( char *fname, char *lname,
109 int bmonth, int bday, int byear,
110 int hmonth, int hday, int hyear )
111 : birthDate( bmonth, bday, byear ),
112 hireDate( hmonth, hday, hyear )
113 {
114 // copy fname into firstName and be sure that it fits
115 int length = strlen( fname );
116 length = ( length < 25 ? length : 24 );
117 strncpy( firstName, fname, length );
118 firstName[ length ] = '\\0';
119
120 // copy lname into lastName and be sure that it fits
121 length = strlen( lname );
122 length = ( length < 25 ? length : 24 );
123 strncpy( lastName, lname, length );
124 lastName[ length ] = '\\0';
125
126 cout << "Employee object constructor: "
127 << firstName << ' ' << lastName << endl;
128 }
129
130 void Employee::print() const
131 {
132 cout << lastName << ", " << firstName << "\nHired: ";
133 hireDate.print();
134 cout << " Birth date: ";
135 birthDate.print();
136 cout << endl;
137 }
138
139 // Destructor: provided to confirm destruction order
140 Employee::~Employee()
141 {
142 cout << "Employee object destructor: "
143 << lastName << ", " << firstName << endl;
144 }
145 // Fig. 7.4: fig07_04.cpp
146 // Demonstrating composition: an object with member objects.
147 #include <iostream.h>
148 #include "emply1.h"
149
150 int main()
151 {
152 Employee e( "Bob", "Jones", 7, 24, 1949, 3, 12, 1988 );
153
154 cout << '\n';
155 e.print();
156
157 cout << "\nTest Date constructor with invalid values:\n";
158 Date d( 14, 35, 1994 ); // invalid Date values
159 cout << endl;
160 return 0;
161 }